Rainfall vs Niño#

https://www.ncei.noaa.gov/data/global-historical-climatology-network-daily/doc/GHCND_documentation.pdf

import warnings
warnings.filterwarnings("ignore")
import os
import sys

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import df2img


sys.path.append("../../../../indicators_setup")
from ind_setup.colors import get_df_col, plotting_style
from ind_setup.tables import plot_df_table
from ind_setup.plotting_int import plot_oni_index_th

plotting_style()
from ind_setup.core import fontsize

sys.path.append("../../../functions")
from data_downloaders import GHCN, download_oni_index

Define location and variables of interest#

country = 'Palau'
vars_interest = ['PRCP']

Get Data#

df_country = GHCN.get_country_code(country)
print(f'The GHCN code for {country} is {df_country["Code"].values[0]}')
The GHCN code for Palau is PS
df_stations = GHCN.download_stations_info()
df_country_stations = df_stations[df_stations['ID'].str.startswith(df_country.Code.values[0])]
print(f'There are {df_country_stations.shape[0]} stations in {country}')
There are 13 stations in Palau
GHCND_dir = 'https://www.ncei.noaa.gov/data/global-historical-climatology-network-daily/access/'

Using Koror Station#

id = 'PSW00040309' # Koror Station
dict_prcp = GHCN.extract_dict_data_var(GHCND_dir, 'PRCP', df_country_stations.loc[df_country_stations['ID'] == id])[0]
st_data = dict_prcp[0]['data'].dropna()

ONI index#

https://origin.cpc.ncep.noaa.gov/products/analysis_monitoring/ensostuff/ONI_v5.php

p_data = 'https://psl.noaa.gov/data/correlation/oni.data'
df1 = download_oni_index(p_data)
lims = [-.5, .5]
plot_oni_index_th(df1, lims = lims)
st_data_monthly = st_data.resample('M').mean()
st_data_monthly.index = pd.DatetimeIndex(st_data_monthly.index).to_period('M').to_timestamp() + pd.offsets.MonthBegin(1)
st_data_monthly
PRCP
DATE
1951-08-01 7.800000
1951-09-01 16.800000
1951-10-01 7.266667
1951-11-01 5.554839
1951-12-01 9.093333
... ...
2024-09-01 7.529032
2024-10-01 13.756667
2024-11-01 7.055172
2024-12-01 7.495238
2025-01-01 9.221429

882 rows × 1 columns

rolling_mean = 6 #months
df1['prcp'] = st_data_monthly['PRCP'].rolling(window=rolling_mean).mean()
fig, ax = plt.subplots(figsize = [15, 6])
df1.ONI.plot(ax = ax, color = get_df_col()[0], lw = 2)

ax2 = ax.twinx()
df1.prcp.plot(ax = ax2, color = get_df_col()[1], lw = 2)
# df1.tmax.plot(ax = ax2, color = get_df_col()[1], lw = 2)
# df1.tdiff.plot(ax = ax2, color = get_df_col()[1], lw = 2)
# df1.tmean.plot(ax = ax2, color = get_df_col()[1], lw = 2)
<Axes: >
../../../_images/c42ea1f54b171063752cd4c00964967643faf250ad25425dcea59105c70ac401.png
low_lim = np.nanmin(df1.prcp)

fig, ax = plt.subplots(figsize = [15, 6])
df1.prcp.plot(ax = ax, color = get_df_col()[1], lw = 2)

ax.fill_between(df1.index, low_lim, df1.prcp, where = (df1.ONI > lims[1]), color = get_df_col()[2],
                 alpha = 0.7, label = f'ONI over th: {lims[1]}')
ax.fill_between(df1.index, low_lim, df1.prcp, where = (df1.ONI < lims[0]), color = get_df_col()[3], 
                alpha = 0.7, label = f'ONI below th: {lims[0]}')

ax.fill_between(df1.index, low_lim, df1.prcp, where = ((df1.ONI > lims[0]) & (df1.ONI < lims[1])),
                 color = get_df_col()[6], alpha = 0.075)

ax.legend(fontsize=fontsize)
ax.set_title('PRECIPITATION and ONI', fontsize = fontsize)
ax.set_ylabel('PRECIP [°C]', fontsize = fontsize)
ax.set_xlabel('Time', fontsize = fontsize)
Text(0.5, 0, 'Time')
../../../_images/d4b004830b3bdca3cf8f705ec693107e38ee81151e5ed7d2b5a050e0367af712.png
plt.figure(figsize=(5, 4))
sns.heatmap(df1.corr(), annot=True, cmap='coolwarm', vmin=-1, vmax=1)
plt.title('Correlation Heatmap')
plt.show()
../../../_images/cabaed191ab459098e065a1450e870557b9f2996c832c22a61ba72e3245d5886.png
df_format = np.round(df1.describe(), 2)
fig = plot_df_table(df_format, figsize = (400, 300))
df2img.save_dataframe(fig=fig, filename="getting_started.png")
../../../_images/3fb34676e8aebf2667cdb4a9a8af21820a442ec7b5078d51a3be7c8fecfdb0c8.png